{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ECOモデルでの推論\n",
    "\n",
    "本ファイルでは、ECOモデルを実装し、動画データのクラス分類を行います\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 9.5 学習目標\n",
    "\n",
    "1.\tECOモデルを実装できる\n",
    "2.\t学習済みのECOモデルを自分のモデルにロードできる\n",
    "3.\tECOモデルを使用して、テストデータの推論ができる\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 事前準備\n",
    "\n",
    "- フォルダ「weights」に「ECO_Lite_rgb_model_Kinetics.pth.tar」をダウンロードして配置してください。\n",
    "\n",
    "https://github.com/mzolfaghari/ECO-pytorch の\n",
    "\n",
    "https://drive.google.com/open?id=1XNIq7byciKgrn011jLBggd2g79jKX4uD\n",
    "\n",
    "\n",
    "- 9.2から9.4節での実装内容をフォルダ「utils」に用意しています。\n",
    "\n",
    "それらを利用します。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "from torch.nn import init"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# フォルダ「weights」が存在しない場合は作成する\n",
    "weights_dir = \"./weights/\"\n",
    "if not os.path.exists(weights_dir):\n",
    "    os.mkdir(weights_dir)\n",
    "\n",
    "# フォルダ「weights」に学習済みモデル「ECO_Lite_rgb_model_Kinetics.pth.tar」をダウンロードして配置してください。\n",
    "# https://github.com/mzolfaghari/ECO-pytorch の\n",
    "# https://drive.google.com/open?id=1XNIq7byciKgrn011jLBggd2g79jKX4uD"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Kinematics動画データセットのDataLoaderを用意"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([8, 16, 3, 224, 224])\n"
     ]
    }
   ],
   "source": [
    "from utils.kinetics400_eco_dataloader import make_datapath_list, VideoTransform, get_label_id_dictionary, VideoDataset\n",
    "\n",
    "# vieo_listの作成\n",
    "root_path = './data/kinetics_videos/'\n",
    "video_list = make_datapath_list(root_path)\n",
    "\n",
    "# 前処理の設定\n",
    "resize, crop_size = 224, 224\n",
    "mean, std = [104, 117, 123], [1, 1, 1]\n",
    "video_transform = VideoTransform(resize, crop_size, mean, std)\n",
    "\n",
    "# ラベル辞書の作成\n",
    "label_dicitionary_path = './video_download/kinetics_400_label_dicitionary.csv'\n",
    "label_id_dict, id_label_dict = get_label_id_dictionary(label_dicitionary_path)\n",
    "\n",
    "# Datasetの作成\n",
    "# num_segments は 動画を何分割して使用するのかを決める\n",
    "val_dataset = VideoDataset(video_list, label_id_dict, num_segments=16,\n",
    "                           phase=\"val\", transform=video_transform, img_tmpl='image_{:05d}.jpg')\n",
    "\n",
    "# DataLoaderにします\n",
    "batch_size = 8\n",
    "val_dataloader = torch.utils.data.DataLoader(\n",
    "    val_dataset, batch_size=batch_size, shuffle=False)\n",
    "\n",
    "# 動作確認\n",
    "batch_iterator = iter(val_dataloader)  # イテレータに変換\n",
    "imgs_transformeds, labels, label_ids, dir_path = next(\n",
    "    batch_iterator)  # 1番目の要素を取り出す\n",
    "print(imgs_transformeds.shape)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ECOモデルを実装"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from utils.eco import ECO_2D, ECO_3D\n",
    "\n",
    "\n",
    "class ECO_Lite(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(ECO_Lite, self).__init__()\n",
    "\n",
    "        # 2D Netモジュール\n",
    "        self.eco_2d = ECO_2D()\n",
    "\n",
    "        # 3D Netモジュール\n",
    "        self.eco_3d = ECO_3D()\n",
    "\n",
    "        # クラス分類の全結合層\n",
    "        self.fc_final = nn.Linear(in_features=512, out_features=400, bias=True)\n",
    "\n",
    "    def forward(self, x):\n",
    "        '''\n",
    "        入力xはtorch.Size([batch_num, num_segments=16, 3, 224, 224]))\n",
    "        '''\n",
    "\n",
    "        # 入力xの各次元のサイズを取得する\n",
    "        bs, ns, c, h, w = x.shape\n",
    "\n",
    "        # xを(bs*ns, c, h, w)にサイズ変換する\n",
    "        out = x.view(-1, c, h, w)\n",
    "        # （注釈）\n",
    "        # PyTorchのConv2Dは入力のサイズが(batch_num, c, h, w)しか受け付けないため\n",
    "        # (batch_num, num_segments, c, h, w)は処理できない\n",
    "        # 今は2次元画像を独立に処理するので、num_segmentsはbatch_numの次元に押し込んでも良いため\n",
    "        # (batch_num×num_segments, c, h, w)にサイズを変換する\n",
    "\n",
    "        # 2D Netモジュール 出力torch.Size([batch_num×16, 96, 28, 28])\n",
    "        out = self.eco_2d(out)\n",
    "\n",
    "        # 2次元画像をテンソルを3次元用に変換する\n",
    "        # num_segmentsをbatch_numの次元に押し込んだものを元に戻す\n",
    "        out = out.view(-1, ns, 96, 28, 28)\n",
    "\n",
    "        # 3D Netモジュール 出力torch.Size([batch_num, 512])\n",
    "        out = self.eco_3d(out)\n",
    "\n",
    "        # クラス分類の全結合層　出力torch.Size([batch_num, class_num=400])\n",
    "        out = self.fc_final(out)\n",
    "\n",
    "        return out\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ECO_Lite(\n",
       "  (eco_2d): ECO_2D(\n",
       "    (basic_conv): BasicConv(\n",
       "      (conv1_7x7_s2): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3))\n",
       "      (conv1_7x7_s2_bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv1_relu_7x7): ReLU(inplace)\n",
       "      (pool1_3x3_s2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=True)\n",
       "      (conv2_3x3_reduce): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1))\n",
       "      (conv2_3x3_reduce_bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2_relu_3x3_reduce): ReLU(inplace)\n",
       "      (conv2_3x3): Conv2d(64, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (conv2_3x3_bn): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2_relu_3x3): ReLU(inplace)\n",
       "      (pool2_3x3_s2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=True)\n",
       "    )\n",
       "    (inception_a): InceptionA(\n",
       "      (inception_3a_1x1): Conv2d(192, 64, kernel_size=(1, 1), stride=(1, 1))\n",
       "      (inception_3a_1x1_bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (inception_3a_relu_1x1): ReLU(inplace)\n",
       "      (inception_3a_3x3_reduce): Conv2d(192, 64, kernel_size=(1, 1), stride=(1, 1))\n",
       "      (inception_3a_3x3_reduce_bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (inception_3a_relu_3x3_reduce): ReLU(inplace)\n",
       "      (inception_3a_3x3): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (inception_3a_3x3_bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (inception_3a_relu_3x3): ReLU(inplace)\n",
       "      (inception_3a_double_3x3_reduce): Conv2d(192, 64, kernel_size=(1, 1), stride=(1, 1))\n",
       "      (inception_3a_double_3x3_reduce_bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (inception_3a_relu_double_3x3_reduce): ReLU(inplace)\n",
       "      (inception_3a_double_3x3_1): Conv2d(64, 96, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (inception_3a_double_3x3_1_bn): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (inception_3a_relu_double_3x3_1): ReLU(inplace)\n",
       "      (inception_3a_double_3x3_2): Conv2d(96, 96, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (inception_3a_double_3x3_2_bn): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (inception_3a_relu_double_3x3_2): ReLU(inplace)\n",
       "      (inception_3a_pool): AvgPool2d(kernel_size=3, stride=1, padding=1)\n",
       "      (inception_3a_pool_proj): Conv2d(192, 32, kernel_size=(1, 1), stride=(1, 1))\n",
       "      (inception_3a_pool_proj_bn): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (inception_3a_relu_pool_proj): ReLU(inplace)\n",
       "    )\n",
       "    (inception_b): InceptionB(\n",
       "      (inception_3b_1x1): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1))\n",
       "      (inception_3b_1x1_bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (inception_3b_relu_1x1): ReLU(inplace)\n",
       "      (inception_3b_3x3_reduce): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1))\n",
       "      (inception_3b_3x3_reduce_bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (inception_3b_relu_3x3_reduce): ReLU(inplace)\n",
       "      (inception_3b_3x3): Conv2d(64, 96, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (inception_3b_3x3_bn): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (inception_3b_relu_3x3): ReLU(inplace)\n",
       "      (inception_3b_double_3x3_reduce): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1))\n",
       "      (inception_3b_double_3x3_reduce_bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (inception_3b_relu_double_3x3_reduce): ReLU(inplace)\n",
       "      (inception_3b_double_3x3_1): Conv2d(64, 96, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (inception_3b_double_3x3_1_bn): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (inception_3b_relu_double_3x3_1): ReLU(inplace)\n",
       "      (inception_3b_double_3x3_2): Conv2d(96, 96, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (inception_3b_double_3x3_2_bn): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (inception_3b_relu_double_3x3_2): ReLU(inplace)\n",
       "      (inception_3b_pool): AvgPool2d(kernel_size=3, stride=1, padding=1)\n",
       "      (inception_3b_pool_proj): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1))\n",
       "      (inception_3b_pool_proj_bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (inception_3b_relu_pool_proj): ReLU(inplace)\n",
       "    )\n",
       "    (inception_c): InceptionC(\n",
       "      (inception_3c_double_3x3_reduce): Conv2d(320, 64, kernel_size=(1, 1), stride=(1, 1))\n",
       "      (inception_3c_double_3x3_reduce_bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (inception_3c_relu_double_3x3_reduce): ReLU(inplace)\n",
       "      (inception_3c_double_3x3_1): Conv2d(64, 96, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (inception_3c_double_3x3_1_bn): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (inception_3c_relu_double_3x3_1): ReLU(inplace)\n",
       "    )\n",
       "  )\n",
       "  (eco_3d): ECO_3D(\n",
       "    (res_3d_3): Resnet_3D_3(\n",
       "      (res3a_2): Conv3d(96, 128, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))\n",
       "      (res3a_bn): BatchNorm3d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (res3a_relu): ReLU(inplace)\n",
       "      (res3b_1): Conv3d(128, 128, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))\n",
       "      (res3b_1_bn): BatchNorm3d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (res3b_1_relu): ReLU(inplace)\n",
       "      (res3b_2): Conv3d(128, 128, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))\n",
       "      (res3b_bn): BatchNorm3d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (res3b_relu): ReLU(inplace)\n",
       "    )\n",
       "    (res_3d_4): Resnet_3D_4(\n",
       "      (res4a_1): Conv3d(128, 256, kernel_size=(3, 3, 3), stride=(2, 2, 2), padding=(1, 1, 1))\n",
       "      (res4a_1_bn): BatchNorm3d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (res4a_1_relu): ReLU(inplace)\n",
       "      (res4a_2): Conv3d(256, 256, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))\n",
       "      (res4a_down): Conv3d(128, 256, kernel_size=(3, 3, 3), stride=(2, 2, 2), padding=(1, 1, 1))\n",
       "      (res4a_bn): BatchNorm3d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (res4a_relu): ReLU(inplace)\n",
       "      (res4b_1): Conv3d(256, 256, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))\n",
       "      (res4b_1_bn): BatchNorm3d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (res4b_1_relu): ReLU(inplace)\n",
       "      (res4b_2): Conv3d(256, 256, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))\n",
       "      (res4b_bn): BatchNorm3d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (res4b_relu): ReLU(inplace)\n",
       "    )\n",
       "    (res_3d_5): Resnet_3D_5(\n",
       "      (res5a_1): Conv3d(256, 512, kernel_size=(3, 3, 3), stride=(2, 2, 2), padding=(1, 1, 1))\n",
       "      (res5a_1_bn): BatchNorm3d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (res5a_1_relu): ReLU(inplace)\n",
       "      (res5a_2): Conv3d(512, 512, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))\n",
       "      (res5a_down): Conv3d(256, 512, kernel_size=(3, 3, 3), stride=(2, 2, 2), padding=(1, 1, 1))\n",
       "      (res5a_bn): BatchNorm3d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (res5a_relu): ReLU(inplace)\n",
       "      (res5b_1): Conv3d(512, 512, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))\n",
       "      (res5b_1_bn): BatchNorm3d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (res5b_1_relu): ReLU(inplace)\n",
       "      (res5b_2): Conv3d(512, 512, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))\n",
       "      (res5b_bn): BatchNorm3d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (res5b_relu): ReLU(inplace)\n",
       "    )\n",
       "    (global_pool): AvgPool3d(kernel_size=(4, 7, 7), stride=1, padding=0)\n",
       "  )\n",
       "  (fc_final): Linear(in_features=512, out_features=400, bias=True)\n",
       ")"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net = ECO_Lite()\n",
    "net"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 学習済みモデルをロード"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "学習済みのパラメータをロードします\n",
      "module.base_model.conv1_7x7_s2.weight→eco_2d.basic_conv.conv1_7x7_s2.weight\n",
      "module.base_model.conv1_7x7_s2.bias→eco_2d.basic_conv.conv1_7x7_s2.bias\n",
      "module.base_model.conv1_7x7_s2_bn.weight→eco_2d.basic_conv.conv1_7x7_s2_bn.weight\n",
      "module.base_model.conv1_7x7_s2_bn.bias→eco_2d.basic_conv.conv1_7x7_s2_bn.bias\n",
      "module.base_model.conv1_7x7_s2_bn.running_mean→eco_2d.basic_conv.conv1_7x7_s2_bn.running_mean\n",
      "module.base_model.conv1_7x7_s2_bn.running_var→eco_2d.basic_conv.conv1_7x7_s2_bn.running_var\n",
      "module.base_model.conv1_7x7_s2_bn.num_batches_tracked→eco_2d.basic_conv.conv1_7x7_s2_bn.num_batches_tracked\n",
      "module.base_model.conv2_3x3_reduce.weight→eco_2d.basic_conv.conv2_3x3_reduce.weight\n",
      "module.base_model.conv2_3x3_reduce.bias→eco_2d.basic_conv.conv2_3x3_reduce.bias\n",
      "module.base_model.conv2_3x3_reduce_bn.weight→eco_2d.basic_conv.conv2_3x3_reduce_bn.weight\n",
      "module.base_model.conv2_3x3_reduce_bn.bias→eco_2d.basic_conv.conv2_3x3_reduce_bn.bias\n",
      "module.base_model.conv2_3x3_reduce_bn.running_mean→eco_2d.basic_conv.conv2_3x3_reduce_bn.running_mean\n",
      "module.base_model.conv2_3x3_reduce_bn.running_var→eco_2d.basic_conv.conv2_3x3_reduce_bn.running_var\n",
      "module.base_model.conv2_3x3_reduce_bn.num_batches_tracked→eco_2d.basic_conv.conv2_3x3_reduce_bn.num_batches_tracked\n",
      "module.base_model.conv2_3x3.weight→eco_2d.basic_conv.conv2_3x3.weight\n",
      "module.base_model.conv2_3x3.bias→eco_2d.basic_conv.conv2_3x3.bias\n",
      "module.base_model.conv2_3x3_bn.weight→eco_2d.basic_conv.conv2_3x3_bn.weight\n",
      "module.base_model.conv2_3x3_bn.bias→eco_2d.basic_conv.conv2_3x3_bn.bias\n",
      "module.base_model.conv2_3x3_bn.running_mean→eco_2d.basic_conv.conv2_3x3_bn.running_mean\n",
      "module.base_model.conv2_3x3_bn.running_var→eco_2d.basic_conv.conv2_3x3_bn.running_var\n",
      "module.base_model.conv2_3x3_bn.num_batches_tracked→eco_2d.basic_conv.conv2_3x3_bn.num_batches_tracked\n",
      "module.base_model.inception_3a_1x1.weight→eco_2d.inception_a.inception_3a_1x1.weight\n",
      "module.base_model.inception_3a_1x1.bias→eco_2d.inception_a.inception_3a_1x1.bias\n",
      "module.base_model.inception_3a_1x1_bn.weight→eco_2d.inception_a.inception_3a_1x1_bn.weight\n",
      "module.base_model.inception_3a_1x1_bn.bias→eco_2d.inception_a.inception_3a_1x1_bn.bias\n",
      "module.base_model.inception_3a_1x1_bn.running_mean→eco_2d.inception_a.inception_3a_1x1_bn.running_mean\n",
      "module.base_model.inception_3a_1x1_bn.running_var→eco_2d.inception_a.inception_3a_1x1_bn.running_var\n",
      "module.base_model.inception_3a_1x1_bn.num_batches_tracked→eco_2d.inception_a.inception_3a_1x1_bn.num_batches_tracked\n",
      "module.base_model.inception_3a_3x3_reduce.weight→eco_2d.inception_a.inception_3a_3x3_reduce.weight\n",
      "module.base_model.inception_3a_3x3_reduce.bias→eco_2d.inception_a.inception_3a_3x3_reduce.bias\n",
      "module.base_model.inception_3a_3x3_reduce_bn.weight→eco_2d.inception_a.inception_3a_3x3_reduce_bn.weight\n",
      "module.base_model.inception_3a_3x3_reduce_bn.bias→eco_2d.inception_a.inception_3a_3x3_reduce_bn.bias\n",
      "module.base_model.inception_3a_3x3_reduce_bn.running_mean→eco_2d.inception_a.inception_3a_3x3_reduce_bn.running_mean\n",
      "module.base_model.inception_3a_3x3_reduce_bn.running_var→eco_2d.inception_a.inception_3a_3x3_reduce_bn.running_var\n",
      "module.base_model.inception_3a_3x3_reduce_bn.num_batches_tracked→eco_2d.inception_a.inception_3a_3x3_reduce_bn.num_batches_tracked\n",
      "module.base_model.inception_3a_3x3.weight→eco_2d.inception_a.inception_3a_3x3.weight\n",
      "module.base_model.inception_3a_3x3.bias→eco_2d.inception_a.inception_3a_3x3.bias\n",
      "module.base_model.inception_3a_3x3_bn.weight→eco_2d.inception_a.inception_3a_3x3_bn.weight\n",
      "module.base_model.inception_3a_3x3_bn.bias→eco_2d.inception_a.inception_3a_3x3_bn.bias\n",
      "module.base_model.inception_3a_3x3_bn.running_mean→eco_2d.inception_a.inception_3a_3x3_bn.running_mean\n",
      "module.base_model.inception_3a_3x3_bn.running_var→eco_2d.inception_a.inception_3a_3x3_bn.running_var\n",
      "module.base_model.inception_3a_3x3_bn.num_batches_tracked→eco_2d.inception_a.inception_3a_3x3_bn.num_batches_tracked\n",
      "module.base_model.inception_3a_double_3x3_reduce.weight→eco_2d.inception_a.inception_3a_double_3x3_reduce.weight\n",
      "module.base_model.inception_3a_double_3x3_reduce.bias→eco_2d.inception_a.inception_3a_double_3x3_reduce.bias\n",
      "module.base_model.inception_3a_double_3x3_reduce_bn.weight→eco_2d.inception_a.inception_3a_double_3x3_reduce_bn.weight\n",
      "module.base_model.inception_3a_double_3x3_reduce_bn.bias→eco_2d.inception_a.inception_3a_double_3x3_reduce_bn.bias\n",
      "module.base_model.inception_3a_double_3x3_reduce_bn.running_mean→eco_2d.inception_a.inception_3a_double_3x3_reduce_bn.running_mean\n",
      "module.base_model.inception_3a_double_3x3_reduce_bn.running_var→eco_2d.inception_a.inception_3a_double_3x3_reduce_bn.running_var\n",
      "module.base_model.inception_3a_double_3x3_reduce_bn.num_batches_tracked→eco_2d.inception_a.inception_3a_double_3x3_reduce_bn.num_batches_tracked\n",
      "module.base_model.inception_3a_double_3x3_1.weight→eco_2d.inception_a.inception_3a_double_3x3_1.weight\n",
      "module.base_model.inception_3a_double_3x3_1.bias→eco_2d.inception_a.inception_3a_double_3x3_1.bias\n",
      "module.base_model.inception_3a_double_3x3_1_bn.weight→eco_2d.inception_a.inception_3a_double_3x3_1_bn.weight\n",
      "module.base_model.inception_3a_double_3x3_1_bn.bias→eco_2d.inception_a.inception_3a_double_3x3_1_bn.bias\n",
      "module.base_model.inception_3a_double_3x3_1_bn.running_mean→eco_2d.inception_a.inception_3a_double_3x3_1_bn.running_mean\n",
      "module.base_model.inception_3a_double_3x3_1_bn.running_var→eco_2d.inception_a.inception_3a_double_3x3_1_bn.running_var\n",
      "module.base_model.inception_3a_double_3x3_1_bn.num_batches_tracked→eco_2d.inception_a.inception_3a_double_3x3_1_bn.num_batches_tracked\n",
      "module.base_model.inception_3a_double_3x3_2.weight→eco_2d.inception_a.inception_3a_double_3x3_2.weight\n",
      "module.base_model.inception_3a_double_3x3_2.bias→eco_2d.inception_a.inception_3a_double_3x3_2.bias\n",
      "module.base_model.inception_3a_double_3x3_2_bn.weight→eco_2d.inception_a.inception_3a_double_3x3_2_bn.weight\n",
      "module.base_model.inception_3a_double_3x3_2_bn.bias→eco_2d.inception_a.inception_3a_double_3x3_2_bn.bias\n",
      "module.base_model.inception_3a_double_3x3_2_bn.running_mean→eco_2d.inception_a.inception_3a_double_3x3_2_bn.running_mean\n",
      "module.base_model.inception_3a_double_3x3_2_bn.running_var→eco_2d.inception_a.inception_3a_double_3x3_2_bn.running_var\n",
      "module.base_model.inception_3a_double_3x3_2_bn.num_batches_tracked→eco_2d.inception_a.inception_3a_double_3x3_2_bn.num_batches_tracked\n",
      "module.base_model.inception_3a_pool_proj.weight→eco_2d.inception_a.inception_3a_pool_proj.weight\n",
      "module.base_model.inception_3a_pool_proj.bias→eco_2d.inception_a.inception_3a_pool_proj.bias\n",
      "module.base_model.inception_3a_pool_proj_bn.weight→eco_2d.inception_a.inception_3a_pool_proj_bn.weight\n",
      "module.base_model.inception_3a_pool_proj_bn.bias→eco_2d.inception_a.inception_3a_pool_proj_bn.bias\n",
      "module.base_model.inception_3a_pool_proj_bn.running_mean→eco_2d.inception_a.inception_3a_pool_proj_bn.running_mean\n",
      "module.base_model.inception_3a_pool_proj_bn.running_var→eco_2d.inception_a.inception_3a_pool_proj_bn.running_var\n",
      "module.base_model.inception_3a_pool_proj_bn.num_batches_tracked→eco_2d.inception_a.inception_3a_pool_proj_bn.num_batches_tracked\n",
      "module.base_model.inception_3b_1x1.weight→eco_2d.inception_b.inception_3b_1x1.weight\n",
      "module.base_model.inception_3b_1x1.bias→eco_2d.inception_b.inception_3b_1x1.bias\n",
      "module.base_model.inception_3b_1x1_bn.weight→eco_2d.inception_b.inception_3b_1x1_bn.weight\n",
      "module.base_model.inception_3b_1x1_bn.bias→eco_2d.inception_b.inception_3b_1x1_bn.bias\n",
      "module.base_model.inception_3b_1x1_bn.running_mean→eco_2d.inception_b.inception_3b_1x1_bn.running_mean\n",
      "module.base_model.inception_3b_1x1_bn.running_var→eco_2d.inception_b.inception_3b_1x1_bn.running_var\n",
      "module.base_model.inception_3b_1x1_bn.num_batches_tracked→eco_2d.inception_b.inception_3b_1x1_bn.num_batches_tracked\n",
      "module.base_model.inception_3b_3x3_reduce.weight→eco_2d.inception_b.inception_3b_3x3_reduce.weight\n",
      "module.base_model.inception_3b_3x3_reduce.bias→eco_2d.inception_b.inception_3b_3x3_reduce.bias\n",
      "module.base_model.inception_3b_3x3_reduce_bn.weight→eco_2d.inception_b.inception_3b_3x3_reduce_bn.weight\n",
      "module.base_model.inception_3b_3x3_reduce_bn.bias→eco_2d.inception_b.inception_3b_3x3_reduce_bn.bias\n",
      "module.base_model.inception_3b_3x3_reduce_bn.running_mean→eco_2d.inception_b.inception_3b_3x3_reduce_bn.running_mean\n",
      "module.base_model.inception_3b_3x3_reduce_bn.running_var→eco_2d.inception_b.inception_3b_3x3_reduce_bn.running_var\n",
      "module.base_model.inception_3b_3x3_reduce_bn.num_batches_tracked→eco_2d.inception_b.inception_3b_3x3_reduce_bn.num_batches_tracked\n",
      "module.base_model.inception_3b_3x3.weight→eco_2d.inception_b.inception_3b_3x3.weight\n",
      "module.base_model.inception_3b_3x3.bias→eco_2d.inception_b.inception_3b_3x3.bias\n",
      "module.base_model.inception_3b_3x3_bn.weight→eco_2d.inception_b.inception_3b_3x3_bn.weight\n",
      "module.base_model.inception_3b_3x3_bn.bias→eco_2d.inception_b.inception_3b_3x3_bn.bias\n",
      "module.base_model.inception_3b_3x3_bn.running_mean→eco_2d.inception_b.inception_3b_3x3_bn.running_mean\n",
      "module.base_model.inception_3b_3x3_bn.running_var→eco_2d.inception_b.inception_3b_3x3_bn.running_var\n",
      "module.base_model.inception_3b_3x3_bn.num_batches_tracked→eco_2d.inception_b.inception_3b_3x3_bn.num_batches_tracked\n",
      "module.base_model.inception_3b_double_3x3_reduce.weight→eco_2d.inception_b.inception_3b_double_3x3_reduce.weight\n",
      "module.base_model.inception_3b_double_3x3_reduce.bias→eco_2d.inception_b.inception_3b_double_3x3_reduce.bias\n",
      "module.base_model.inception_3b_double_3x3_reduce_bn.weight→eco_2d.inception_b.inception_3b_double_3x3_reduce_bn.weight\n",
      "module.base_model.inception_3b_double_3x3_reduce_bn.bias→eco_2d.inception_b.inception_3b_double_3x3_reduce_bn.bias\n",
      "module.base_model.inception_3b_double_3x3_reduce_bn.running_mean→eco_2d.inception_b.inception_3b_double_3x3_reduce_bn.running_mean\n",
      "module.base_model.inception_3b_double_3x3_reduce_bn.running_var→eco_2d.inception_b.inception_3b_double_3x3_reduce_bn.running_var\n",
      "module.base_model.inception_3b_double_3x3_reduce_bn.num_batches_tracked→eco_2d.inception_b.inception_3b_double_3x3_reduce_bn.num_batches_tracked\n",
      "module.base_model.inception_3b_double_3x3_1.weight→eco_2d.inception_b.inception_3b_double_3x3_1.weight\n",
      "module.base_model.inception_3b_double_3x3_1.bias→eco_2d.inception_b.inception_3b_double_3x3_1.bias\n",
      "module.base_model.inception_3b_double_3x3_1_bn.weight→eco_2d.inception_b.inception_3b_double_3x3_1_bn.weight\n",
      "module.base_model.inception_3b_double_3x3_1_bn.bias→eco_2d.inception_b.inception_3b_double_3x3_1_bn.bias\n",
      "module.base_model.inception_3b_double_3x3_1_bn.running_mean→eco_2d.inception_b.inception_3b_double_3x3_1_bn.running_mean\n",
      "module.base_model.inception_3b_double_3x3_1_bn.running_var→eco_2d.inception_b.inception_3b_double_3x3_1_bn.running_var\n",
      "module.base_model.inception_3b_double_3x3_1_bn.num_batches_tracked→eco_2d.inception_b.inception_3b_double_3x3_1_bn.num_batches_tracked\n",
      "module.base_model.inception_3b_double_3x3_2.weight→eco_2d.inception_b.inception_3b_double_3x3_2.weight\n",
      "module.base_model.inception_3b_double_3x3_2.bias→eco_2d.inception_b.inception_3b_double_3x3_2.bias\n",
      "module.base_model.inception_3b_double_3x3_2_bn.weight→eco_2d.inception_b.inception_3b_double_3x3_2_bn.weight\n",
      "module.base_model.inception_3b_double_3x3_2_bn.bias→eco_2d.inception_b.inception_3b_double_3x3_2_bn.bias\n",
      "module.base_model.inception_3b_double_3x3_2_bn.running_mean→eco_2d.inception_b.inception_3b_double_3x3_2_bn.running_mean\n",
      "module.base_model.inception_3b_double_3x3_2_bn.running_var→eco_2d.inception_b.inception_3b_double_3x3_2_bn.running_var\n",
      "module.base_model.inception_3b_double_3x3_2_bn.num_batches_tracked→eco_2d.inception_b.inception_3b_double_3x3_2_bn.num_batches_tracked\n",
      "module.base_model.inception_3b_pool_proj.weight→eco_2d.inception_b.inception_3b_pool_proj.weight\n",
      "module.base_model.inception_3b_pool_proj.bias→eco_2d.inception_b.inception_3b_pool_proj.bias\n",
      "module.base_model.inception_3b_pool_proj_bn.weight→eco_2d.inception_b.inception_3b_pool_proj_bn.weight\n",
      "module.base_model.inception_3b_pool_proj_bn.bias→eco_2d.inception_b.inception_3b_pool_proj_bn.bias\n",
      "module.base_model.inception_3b_pool_proj_bn.running_mean→eco_2d.inception_b.inception_3b_pool_proj_bn.running_mean\n",
      "module.base_model.inception_3b_pool_proj_bn.running_var→eco_2d.inception_b.inception_3b_pool_proj_bn.running_var\n",
      "module.base_model.inception_3b_pool_proj_bn.num_batches_tracked→eco_2d.inception_b.inception_3b_pool_proj_bn.num_batches_tracked\n",
      "module.base_model.inception_3c_double_3x3_reduce.weight→eco_2d.inception_c.inception_3c_double_3x3_reduce.weight\n",
      "module.base_model.inception_3c_double_3x3_reduce.bias→eco_2d.inception_c.inception_3c_double_3x3_reduce.bias\n",
      "module.base_model.inception_3c_double_3x3_reduce_bn.weight→eco_2d.inception_c.inception_3c_double_3x3_reduce_bn.weight\n",
      "module.base_model.inception_3c_double_3x3_reduce_bn.bias→eco_2d.inception_c.inception_3c_double_3x3_reduce_bn.bias\n",
      "module.base_model.inception_3c_double_3x3_reduce_bn.running_mean→eco_2d.inception_c.inception_3c_double_3x3_reduce_bn.running_mean\n",
      "module.base_model.inception_3c_double_3x3_reduce_bn.running_var→eco_2d.inception_c.inception_3c_double_3x3_reduce_bn.running_var\n",
      "module.base_model.inception_3c_double_3x3_reduce_bn.num_batches_tracked→eco_2d.inception_c.inception_3c_double_3x3_reduce_bn.num_batches_tracked\n",
      "module.base_model.inception_3c_double_3x3_1.weight→eco_2d.inception_c.inception_3c_double_3x3_1.weight\n",
      "module.base_model.inception_3c_double_3x3_1.bias→eco_2d.inception_c.inception_3c_double_3x3_1.bias\n",
      "module.base_model.inception_3c_double_3x3_1_bn.weight→eco_2d.inception_c.inception_3c_double_3x3_1_bn.weight\n",
      "module.base_model.inception_3c_double_3x3_1_bn.bias→eco_2d.inception_c.inception_3c_double_3x3_1_bn.bias\n",
      "module.base_model.inception_3c_double_3x3_1_bn.running_mean→eco_2d.inception_c.inception_3c_double_3x3_1_bn.running_mean\n",
      "module.base_model.inception_3c_double_3x3_1_bn.running_var→eco_2d.inception_c.inception_3c_double_3x3_1_bn.running_var\n",
      "module.base_model.inception_3c_double_3x3_1_bn.num_batches_tracked→eco_2d.inception_c.inception_3c_double_3x3_1_bn.num_batches_tracked\n",
      "module.base_model.res3a_2.weight→eco_3d.res_3d_3.res3a_2.weight\n",
      "module.base_model.res3a_2.bias→eco_3d.res_3d_3.res3a_2.bias\n",
      "module.base_model.res3a_bn.weight→eco_3d.res_3d_3.res3a_bn.weight\n",
      "module.base_model.res3a_bn.bias→eco_3d.res_3d_3.res3a_bn.bias\n",
      "module.base_model.res3a_bn.running_mean→eco_3d.res_3d_3.res3a_bn.running_mean\n",
      "module.base_model.res3a_bn.running_var→eco_3d.res_3d_3.res3a_bn.running_var\n",
      "module.base_model.res3a_bn.num_batches_tracked→eco_3d.res_3d_3.res3a_bn.num_batches_tracked\n",
      "module.base_model.res3b_1.weight→eco_3d.res_3d_3.res3b_1.weight\n",
      "module.base_model.res3b_1.bias→eco_3d.res_3d_3.res3b_1.bias\n",
      "module.base_model.res3b_1_bn.weight→eco_3d.res_3d_3.res3b_1_bn.weight\n",
      "module.base_model.res3b_1_bn.bias→eco_3d.res_3d_3.res3b_1_bn.bias\n",
      "module.base_model.res3b_1_bn.running_mean→eco_3d.res_3d_3.res3b_1_bn.running_mean\n",
      "module.base_model.res3b_1_bn.running_var→eco_3d.res_3d_3.res3b_1_bn.running_var\n",
      "module.base_model.res3b_1_bn.num_batches_tracked→eco_3d.res_3d_3.res3b_1_bn.num_batches_tracked\n",
      "module.base_model.res3b_2.weight→eco_3d.res_3d_3.res3b_2.weight\n",
      "module.base_model.res3b_2.bias→eco_3d.res_3d_3.res3b_2.bias\n",
      "module.base_model.res3b_bn.weight→eco_3d.res_3d_3.res3b_bn.weight\n",
      "module.base_model.res3b_bn.bias→eco_3d.res_3d_3.res3b_bn.bias\n",
      "module.base_model.res3b_bn.running_mean→eco_3d.res_3d_3.res3b_bn.running_mean\n",
      "module.base_model.res3b_bn.running_var→eco_3d.res_3d_3.res3b_bn.running_var\n",
      "module.base_model.res3b_bn.num_batches_tracked→eco_3d.res_3d_3.res3b_bn.num_batches_tracked\n",
      "module.base_model.res4a_1.weight→eco_3d.res_3d_4.res4a_1.weight\n",
      "module.base_model.res4a_1.bias→eco_3d.res_3d_4.res4a_1.bias\n",
      "module.base_model.res4a_1_bn.weight→eco_3d.res_3d_4.res4a_1_bn.weight\n",
      "module.base_model.res4a_1_bn.bias→eco_3d.res_3d_4.res4a_1_bn.bias\n",
      "module.base_model.res4a_1_bn.running_mean→eco_3d.res_3d_4.res4a_1_bn.running_mean\n",
      "module.base_model.res4a_1_bn.running_var→eco_3d.res_3d_4.res4a_1_bn.running_var\n",
      "module.base_model.res4a_1_bn.num_batches_tracked→eco_3d.res_3d_4.res4a_1_bn.num_batches_tracked\n",
      "module.base_model.res4a_2.weight→eco_3d.res_3d_4.res4a_2.weight\n",
      "module.base_model.res4a_2.bias→eco_3d.res_3d_4.res4a_2.bias\n",
      "module.base_model.res4a_down.weight→eco_3d.res_3d_4.res4a_down.weight\n",
      "module.base_model.res4a_down.bias→eco_3d.res_3d_4.res4a_down.bias\n",
      "module.base_model.res4a_bn.weight→eco_3d.res_3d_4.res4a_bn.weight\n",
      "module.base_model.res4a_bn.bias→eco_3d.res_3d_4.res4a_bn.bias\n",
      "module.base_model.res4a_bn.running_mean→eco_3d.res_3d_4.res4a_bn.running_mean\n",
      "module.base_model.res4a_bn.running_var→eco_3d.res_3d_4.res4a_bn.running_var\n",
      "module.base_model.res4a_bn.num_batches_tracked→eco_3d.res_3d_4.res4a_bn.num_batches_tracked\n",
      "module.base_model.res4b_1.weight→eco_3d.res_3d_4.res4b_1.weight\n",
      "module.base_model.res4b_1.bias→eco_3d.res_3d_4.res4b_1.bias\n",
      "module.base_model.res4b_1_bn.weight→eco_3d.res_3d_4.res4b_1_bn.weight\n",
      "module.base_model.res4b_1_bn.bias→eco_3d.res_3d_4.res4b_1_bn.bias\n",
      "module.base_model.res4b_1_bn.running_mean→eco_3d.res_3d_4.res4b_1_bn.running_mean\n",
      "module.base_model.res4b_1_bn.running_var→eco_3d.res_3d_4.res4b_1_bn.running_var\n",
      "module.base_model.res4b_1_bn.num_batches_tracked→eco_3d.res_3d_4.res4b_1_bn.num_batches_tracked\n",
      "module.base_model.res4b_2.weight→eco_3d.res_3d_4.res4b_2.weight\n",
      "module.base_model.res4b_2.bias→eco_3d.res_3d_4.res4b_2.bias\n",
      "module.base_model.res4b_bn.weight→eco_3d.res_3d_4.res4b_bn.weight\n",
      "module.base_model.res4b_bn.bias→eco_3d.res_3d_4.res4b_bn.bias\n",
      "module.base_model.res4b_bn.running_mean→eco_3d.res_3d_4.res4b_bn.running_mean\n",
      "module.base_model.res4b_bn.running_var→eco_3d.res_3d_4.res4b_bn.running_var\n",
      "module.base_model.res4b_bn.num_batches_tracked→eco_3d.res_3d_4.res4b_bn.num_batches_tracked\n",
      "module.base_model.res5a_1.weight→eco_3d.res_3d_5.res5a_1.weight\n",
      "module.base_model.res5a_1.bias→eco_3d.res_3d_5.res5a_1.bias\n",
      "module.base_model.res5a_1_bn.weight→eco_3d.res_3d_5.res5a_1_bn.weight\n",
      "module.base_model.res5a_1_bn.bias→eco_3d.res_3d_5.res5a_1_bn.bias\n",
      "module.base_model.res5a_1_bn.running_mean→eco_3d.res_3d_5.res5a_1_bn.running_mean\n",
      "module.base_model.res5a_1_bn.running_var→eco_3d.res_3d_5.res5a_1_bn.running_var\n",
      "module.base_model.res5a_1_bn.num_batches_tracked→eco_3d.res_3d_5.res5a_1_bn.num_batches_tracked\n",
      "module.base_model.res5a_2.weight→eco_3d.res_3d_5.res5a_2.weight\n",
      "module.base_model.res5a_2.bias→eco_3d.res_3d_5.res5a_2.bias\n",
      "module.base_model.res5a_down.weight→eco_3d.res_3d_5.res5a_down.weight\n",
      "module.base_model.res5a_down.bias→eco_3d.res_3d_5.res5a_down.bias\n",
      "module.base_model.res5a_bn.weight→eco_3d.res_3d_5.res5a_bn.weight\n",
      "module.base_model.res5a_bn.bias→eco_3d.res_3d_5.res5a_bn.bias\n",
      "module.base_model.res5a_bn.running_mean→eco_3d.res_3d_5.res5a_bn.running_mean\n",
      "module.base_model.res5a_bn.running_var→eco_3d.res_3d_5.res5a_bn.running_var\n",
      "module.base_model.res5a_bn.num_batches_tracked→eco_3d.res_3d_5.res5a_bn.num_batches_tracked\n",
      "module.base_model.res5b_1.weight→eco_3d.res_3d_5.res5b_1.weight\n",
      "module.base_model.res5b_1.bias→eco_3d.res_3d_5.res5b_1.bias\n",
      "module.base_model.res5b_1_bn.weight→eco_3d.res_3d_5.res5b_1_bn.weight\n",
      "module.base_model.res5b_1_bn.bias→eco_3d.res_3d_5.res5b_1_bn.bias\n",
      "module.base_model.res5b_1_bn.running_mean→eco_3d.res_3d_5.res5b_1_bn.running_mean\n",
      "module.base_model.res5b_1_bn.running_var→eco_3d.res_3d_5.res5b_1_bn.running_var\n",
      "module.base_model.res5b_1_bn.num_batches_tracked→eco_3d.res_3d_5.res5b_1_bn.num_batches_tracked\n",
      "module.base_model.res5b_2.weight→eco_3d.res_3d_5.res5b_2.weight\n",
      "module.base_model.res5b_2.bias→eco_3d.res_3d_5.res5b_2.bias\n",
      "module.base_model.res5b_bn.weight→eco_3d.res_3d_5.res5b_bn.weight\n",
      "module.base_model.res5b_bn.bias→eco_3d.res_3d_5.res5b_bn.bias\n",
      "module.base_model.res5b_bn.running_mean→eco_3d.res_3d_5.res5b_bn.running_mean\n",
      "module.base_model.res5b_bn.running_var→eco_3d.res_3d_5.res5b_bn.running_var\n",
      "module.base_model.res5b_bn.num_batches_tracked→eco_3d.res_3d_5.res5b_bn.num_batches_tracked\n",
      "module.new_fc.weight→fc_final.weight\n",
      "module.new_fc.bias→fc_final.bias\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "IncompatibleKeys(missing_keys=[], unexpected_keys=[])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 学習済みモデルをロードする関数の定義\n",
    "\n",
    "\n",
    "def load_pretrained_ECO(model_dict, pretrained_model_dict):\n",
    "    '''ECOの学習済みモデルをロードする関数\n",
    "    今回構築したECOは学習済みモデルとレイヤーの順番は同じだが名前が異なる\n",
    "    '''\n",
    "\n",
    "    # 現在のネットワークモデルのパラメータ名\n",
    "    param_names = []  # パラメータの名前を格納していく\n",
    "    for name, param in model_dict.items():\n",
    "        param_names.append(name)\n",
    "\n",
    "    # 現在のネットワークの情報をコピーして新たなstate_dictを作成\n",
    "    new_state_dict = model_dict.copy()\n",
    "\n",
    "    # 新たなstate_dictに学習済みの値を代入\n",
    "    print(\"学習済みのパラメータをロードします\")\n",
    "    for index, (key_name, value) in enumerate(pretrained_model_dict.items()):\n",
    "        name = param_names[index]  # 現在のネットワークでのパラメータ名を取得\n",
    "        new_state_dict[name] = value  # 値を入れる\n",
    "\n",
    "        # 何から何にロードされたのかを表示\n",
    "        print(str(key_name)+\"→\"+str(name))\n",
    "\n",
    "    return new_state_dict\n",
    "\n",
    "\n",
    "# 学習済みモデルをロード\n",
    "net_model_ECO = \"./weights/ECO_Lite_rgb_model_Kinetics.pth.tar\"\n",
    "pretrained_model = torch.load(net_model_ECO, map_location='cpu')\n",
    "pretrained_model_dict = pretrained_model['state_dict']\n",
    "# （注釈）\n",
    "# pthがtarで圧縮されているのは、state_dict以外の情報も一緒に保存されているため。\n",
    "# そのため読み込むときは辞書型変数になっているので['state_dict']で指定する。\n",
    "\n",
    "# 現在のモデルの変数名などを取得\n",
    "model_dict = net.state_dict()\n",
    "\n",
    "# 学習済みモデルのstate_dictを取得\n",
    "new_state_dict = load_pretrained_ECO(model_dict, pretrained_model_dict)\n",
    "\n",
    "# 学習済みモデルのパラメータを代入\n",
    "net.eval()  # ECOネットワークを推論モードに\n",
    "net.load_state_dict(new_state_dict)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 推論（動画データのクラス分類）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([8, 400])\n"
     ]
    }
   ],
   "source": [
    "# 推論します\n",
    "net.eval()  # ECOネットワークを推論モードに\n",
    "\n",
    "batch_iterator = iter(val_dataloader)  # イテレータに変換\n",
    "imgs_transformeds, labels, label_ids, dir_path = next(\n",
    "    batch_iterator)  # 1番目の要素を取り出す\n",
    "\n",
    "with torch.set_grad_enabled(False):\n",
    "    outputs = net(imgs_transformeds)  # ECOで推論\n",
    "\n",
    "print(outputs.shape)  # 出力のサイズ\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ファイル： ./data/kinetics_videos/arm wrestling/C4lCVBZ3ux0_000028_000038\n",
      "予測第1位：arm wrestling\n",
      "予測第2位：headbutting\n",
      "予測第3位：stretching leg\n",
      "予測第4位：shaking hands\n",
      "予測第5位：tai chi\n"
     ]
    }
   ],
   "source": [
    "# 予測結果の上位5つを表示します\n",
    "def show_eco_inference_result(dir_path, outputs_input, id_label_dict, idx=0):\n",
    "    '''ミニバッチの各データに対して、推論結果の上位を出力する関数を定義'''\n",
    "    print(\"ファイル：\", dir_path[idx])  # ファイル名\n",
    "\n",
    "    outputs = outputs_input.clone()  # コピーを作成\n",
    "\n",
    "    for i in range(5):\n",
    "        '''1位から5位までを表示'''\n",
    "        output = outputs[idx]\n",
    "        _, pred = torch.max(output, dim=0)  # 確率最大値のラベルを予測\n",
    "        class_idx = int(pred.numpy())  # クラスIDを出力\n",
    "        print(\"予測第{}位：{}\".format(i+1, id_label_dict[class_idx]))\n",
    "        outputs[idx][class_idx] = -1000  # 最大値だったものを消す（小さくする）\n",
    "\n",
    "\n",
    "# 予測を実施\n",
    "idx = 0\n",
    "show_eco_inference_result(dir_path, outputs, id_label_dict, idx)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ファイル： ./data/kinetics_videos/bungee jumping/TUvSX0pYu4o_000002_000012\n",
      "予測第1位：bungee jumping\n",
      "予測第2位：trapezing\n",
      "予測第3位：abseiling\n",
      "予測第4位：swinging on something\n",
      "予測第5位：climbing a rope\n"
     ]
    }
   ],
   "source": [
    "# 予測を実施\n",
    "idx = 4\n",
    "show_eco_inference_result(dir_path, outputs, id_label_dict, idx)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以上"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
